﻿#include "StoneGolem.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../SpellObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    unsigned char StoneGolem::getAttackRange() const
    {
        return 4;
    }

    StoneGolem::StoneGolem(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool StoneGolem::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), getAttackRange());
    }

    void StoneGolem::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        if (!ranged)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->Type = 0;
            Broadcast(tempVar);

            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::AC});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectAttack *tempVar2 = new S::ObjectAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->Type = 1;
            Broadcast(tempVar2);

            auto location = Functions::PointMove(getCurrentLocation(), getDirection(), 3);

            for (int y = location->Y - 2; y <= location->Y + 2; y++)
            {
                if (y < 0)
                {
                    continue;
                }
                if (y >= getCurrentMap()->Height)
                {
                    break;
                }

                for (int x = location->X - 2; x <= location->X + 2; x++)
                {
                    if (x < 0)
                    {
                        continue;
                    }
                    if (x >= getCurrentMap()->Width)
                    {
                        break;
                    }

                    auto cell = getCurrentMap()->GetCell(x, y);

                    if (!cell->getValid())
                    {
                        continue;
                    }

                    int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MinDC]);

                    auto start = 500;

                    SpellObject *ob = new SpellObject();
                    ob->Spell = Spell::StoneGolemQuake;
                    ob->Value = damage;
                    ob->ExpireTime = getEnvir()->getTime() + 800 + start;
                    ob->TickSpeed = 1000;
                    ob->setDirection(getDirection());
                    Point tempVar3(x, y);
                    ob->setCurrentLocation(&tempVar3);
                    ob->CastLocation = getCurrentLocation();
                    ob->Show = location->X == x && location->Y == y;
                    ob->setCurrentMap(getCurrentMap());

                    S::MapEffect *tempVar4 = new S::MapEffect();
                    tempVar4->Effect = SpellEffect::Tester;
                    tempVar4->Location = new Point(x, y);
                    tempVar4->Value = static_cast<unsigned char>(getDirection());
                    Broadcast(tempVar4);

                    DelayedAction *action = new DelayedAction(DelayedType::Spawn, getEnvir()->getTime() + start, {ob});
                    getCurrentMap()->ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar4' statement was not added since tempVar4 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete ob' statement was not added since ob was passed to a method or constructor. Handle memory management manually.
                }
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }
    }
}
